### DMSA run ###

set TOP_DIR $sh_launch_dir

source -e -v ${TOP_DIR}/scripts/block_setup.tcl
source -e -v ${TOP_DIR}/scripts/pt_setup.tcl

### dmsa host and license number setting
set dmsa_num_of_hosts [expr [llength $dmsa(corners)] * [llength $dmsa(modes)]]
set_host_options -num_processes $dmsa_num_of_hosts -max_cores $dmsa_core_num

### change scenario-based licensing to core-based, each 32 core use one license.
set_app_var multi_scenario_license_mode core

### enable compute resource efficient ECO
set eco_enable_more_scenarios_than_hosts true

### set dmsa working directory and error file
if {[file exist DMSA]} {
		file delete -force DMSA
}
file mkdir DMSA

set multi_scenario_working_directory $TOP_DIR/DMSA
set multi_scenario_merged_error_log $TOP_DIR/DMSA/error.log

### create scenario
foreach corner $dmsa(corners) {
		file delete -force ./$corner
		create_scenario \
  		-name ${corner} \
   		-specific_variables {METHOD SI_ENABLE SESSION_SAVE SHELL_QUIT TOP_DIR corner dmsa(fix_pba_mode)} \
  		-specific_data "${TOP_DIR}/scripts/block_setup.tcl
						${TOP_DIR}/scripts/lib_setup.tcl
						${TOP_DIR}/scripts/pt_setup.tcl
						${TOP_DIR}/scripts/run_pt.tcl"
}

### Do analysis on remote machines
start_hosts
current_session -all
report_multi_scenario_design -scenario -session

### ECO settings
set eco_report_unfixed_reason_max_endpoints 10

set TIME [sh date +%Y_%m_%d_%H_%M]
set eco_instance_name_prefix "DMSA_ECO_CELL_$TIME"
set eco_net_name_prefix "DMSA_ECO_NET_$TIME"

### Enables ECOs with multiply instantiated modules (MIMs)
set_app_var eco_enable_mim true

### start ECO fix 
remote_execute {
		set clk_port [filter_collection [get_attribute [get_clock] sources] object_class==port]
		set_false_path -from [remove_from_collection [all_inputs] [get_ports $clk_port]]
		set_false_path -to [all_outputs]

		set user_filler_cells [get_attr [get_lib_cells {*/DCAP* */FILL*}] base_name]
		#set_eco_options -physical_lib_path ${LIB_LEF_FLIES} -filler_cell_names $user_filler_cells -physical_design_path $DEF -log_file ${TOP_DIR}/DMSA/eco_lef_def.log
		check_eco

		source -e -v ${TOP_DIR}/scripts/report.tcl
		report_annotated_parasitics -check > ./annotation.rep
		check_timing  > ./check_timing.rep

		report_summary pre_eco ${dmsa(fix_pba_mode)}
}


### drc fix
if {[regexp max_transition|max_capacitance|max_fanout|noise ${dmsa(fix_drc_type)}]} {
		foreach drc_type ${dmsa(fix_drc_type)} {
				puts "== INFO: start $drc_type fix ."
				fix_eco_drc -type ${drc_type} -methods {size_cell insert_buffer} -buffer_list $eco_drc_buf -verbose
		}
		remote_execute {
				report_summary post_drc_fix ${dmsa(fix_pba_type)}
		}
}

### setup fix
if {[regexp setup ${dmsa(fix_timing_type)}]} {
		puts "== INFO: start setup fix ."
		fix_eco_timing -type setup -slack_lesser_than ${dmsa(fix_setup_slack)} -setup_margin ${dmsa(fix_setup_setup_margin)} -pba_mode ${dmsa(fix_pba_mode)} -verbose
		remote_execute {
				report_summary post_setup_fix ${dmsa(fix_pba_type)}
		}
}

### hold fix
if {[regexp hold ${dmsa(fix_timing_type)}]} {
		puts "== INFO: start hold fix ."
		fix_eco_timing -type hold -slack_lesser_than ${dmsa(fix_hold_slack)} -setup_margin ${dmsa(fix_hold_setup_margin)} -methods ${dmsa(fix_hold_method)} -buffer_list $eco_hold_buf -pba_mode ${dmsa(fix_pba_mode)} -physical_mode ${dmsa(fix_pba_mode)} -verbose
		remote_execute {
				report_summary post_hold_fix ${dmsa(fix_pba_type)}
		}
}

### write change
write_changes -format icctcl -output ./DMSA/eco_change.tcl

### save session
remote_execute {
		if {$SESSION_SAVE} {
				save_session session
		}
}

### quit shell
if {$SHELL_QUIT} {
		exit
}

